<?php
/*****************************************************************
 * Fusion.Board
 *
 * @author                Denis Wrobel
 * @license               http://www.gnu.org/licenses/gpl-3.0.html
 * @version               0.1
 *
 *****************************************************************/

if( !defined( 'IN_SCRIPT' ) )
{
    die( 'Go Away!' );
}

$modes = array( 'view', 'logout', 'addfriend', 'removefriend' );
$mode = '';
if( !$core->_GET( 'mode' ) || !in_array( $core->_GET( 'mode' ), $modes ) )
{
    $mode = 'panel';
}
else
{
    $mode = $core->_GET( 'mode' );
}

if( $mode == 'logout'  )
{
    if( $session['session_logged'] )
    {
        $logout = $user->logout();
        
        if( $logout )
        {
            $page->redirect( 'index.php', $language->string( 'you_have_logged_out' ) );
        }
        else
        {
            $page->error( 'Error while logging ' );
        }
    }
    else
    {
        $page->error( $language->string( 'you_need_to_be_logged_in' ) );
    }

}
elseif( $mode == 'panel' )
{
    if( !$session['session_logged'] )
    {
        $page->error( $language->string( 'you_need_to_be_logged_in' ) );
    }
    
    //Menu
    $template->assignVars( array( 'l_menu'          => $language->string( 'menu' ),
                                  'l_messenger'     => $language->string( 'messenger' ),
                                  'a_new_message'   => 'index.php?action=messenger&mode=new',
                                  'l_new_message'   => $language->string( 'send_pm' ),
                                  'a_inbox'         => 'index.php?action=messenger&mode=in',
                                  'l_inbox'         => $language->string( 'inbox' ),
                                  'a_outbox'        => 'index.php?action=messenger&mode=out',
                                  'l_outbox'        => $language->string( 'outbox' ),
                                  'l_profile'       => $language->string( 'profile_options' ),
                                  'a_main_settings' => 'index.php?action=usercp&mode=edit',
                                  'l_main_settings' => $language->string( 'profile_main_settings' ) ) );
    
    $page->addBreadcrumb( 'index.php?action=members', $language->string( 'users' ) );
    
    $page->drawHeader();
    $template->draw( 'user_panel' );
    $page->drawFooter();
}
elseif( $mode == 'view' )
{
    $page->title = $language->string( 'user_profile' );
    $page->addBreadcrumb( 'index.php?action=members', $language->string( 'users' ) . ' ' . $core->htmlFilter( $userdata['username'] ) );
    
    
    $uid = (int) $core->_GET( 'u' );
    
    $userdata = $user->userdata( $uid );
    
    
    //User exists?
    if( !$userdata )
    {
        $page->error( $language->string( 'user_does_not_exists' ) );
    }
    
    //Group imagE?
    if( $userdata['group_image'] )
    {
        $template->setIf( 'group_image' );
    }
    else
    {
        $template->setIf( 'show_rank' );
    }
    
    $userDaysOnForum = ( $core->daysAgoTimeStamp( $userdata['join_date'] ) ) ? $core->daysAgoTimeStamp( $userdata['join_date'] ) : '1'; //User days on forum
    $postsPerDay = round( $userdata['user_posts'] / $userDaysOnForum, 2 );
    
    $template->assignVars( array( 'l_profile'        => $language->string( 'user_profile' ),
                                  'username'         => $core->colorUsername( $userdata['group_style'], $core->htmlFilter( $userdata['username'] ) ),
                                  'avatar'           => ( $userdata['user_avatar'] ) ? '' : $template->assignBlock( 'no_avatar' ),
                                  'group_image'      => $core->htmlFilter( $userdata['group_image'] ),
                                  'group_alt'        => $core->htmlFilter( $userdata['group_name'] ),
                                  'rank_title'       => $core->htmlFilter( $userdata['rank_title'] ),
                                  'rank_image'       => $core->htmlFilter( $userdata['rank_image'] ),
                                  'group_name'       => $core->colorUsername( $userdata['group_style'], $core->htmlFilter( $userdata['group_name'] ) ),
                                  'l_group'          => $language->string( 'user_group' ),
                                  'join_date'        => $core->formatTimeStamp( $userdata['join_date'] ),
                                  'l_joined'         => $language->string( 'joined' ),
                                  'l_contact'        => $language->string( 'contact' ),
                                  'l_send_pm'        => $language->string( 'send_pm' ),
                                  'a_send_pm'        => 'index.php?action=messenger&mode=new&u=' . $uid,
                                  'l_send_email'     => $language->string( 'send_email' ),
                                  'a_send_email'     => 'mailto:' . $core->htmlFilter( $userdata['user_email'] ),
                                  'l_forum_activity' => $language->string( 'forum_activity' ),
                                  'l_posts'          => $language->string( 'user_posts' ),
                                  'user_posts'       => $userdata['user_posts'],
                                  'posts_per_day'    => $postsPerDay,
                                  'l_posts_per_day'  => $language->string( 'posts_per_day', 0 ),
                                  'l_last_activity'  => $language->string( 'last_activity' ),
                                  'last_login'       => $core->formatTimeStamp( $userdata['last_login'] ),
                                  'l_user_status'    => $language->string( 'user_status' ),
                                  'user_status'      => ( $user->userStatus( $uid ) ) ? $language->string( 'online' ) : $language->string( 'offline' ),
                                  'l_extra_fields'   => $language->string( 'extra_fields' ),
                                  'l_personal_info'  => $language->string( 'personal_info' ),
                                  'l_name'           => $language->string( 'name' ),
                                  'user_name'        => ( $userdata['user_name'] ) ? $core->htmlFilter( $userdata['user_name'] ) : $language->string( 'not_set' ),
                                  'l_localization'   => $language->string( 'localization' ),
                                  'user_localization'=> ( $userdata['user_localization'] ) ? $core->htmlFilter( $userdata['user_localization'] ) : $language->string( 'not_set' ),
                                  'l_hobby'          => $language->string( 'hobby' ),
                                  'user_hobby'       => ( $userdata['user_hobby'] ) ? $core->htmlFilter( $userdata['user_hobby'] ) : $language->string( 'not_set' ),
                                  'l_profession'     => $language->string( 'profession' ),
                                  'user_profession'  => ( $userdata['user_profession'] ) ? $core->htmlFilter( $userdata['user_profession'] ) : $language->string( 'not_set' ),
                                  'l_others'         => $language->string( 'others' ),
                                  'l_add_friend'     => $language->string( 'add_friend' ),
                                  'a_add_friend'     => 'index.php?action=usercp&mode=addfriend&buddy=' . $uid,
                                  'l_friends'        => $language->string( 'friends' ),
                                  'l_no_friends'     => $language->string( 'user_not_added_any_friend' ),
                                  'l_remove_friend'  => $language->string( 'remove_friend' )
                                  ) );
    
    
    //Extra fields
    $extraFields = $core->userExtraFields( $uid, 'profile_view' );
    
    if( count( $extraFields ) )
    {
        foreach( $extraFields AS $extraField )
        {
            $template->setLoop( 'extra_fields', array( 'field_name'  => $core->htmlFilter( $extraField['field_name'] ),
                                                       'field_value' => ( strlen( $extraField['field_value'] ) ) ? $core->htmlFilter( $extraField['field_value'] ) : $language->string( 'not_set' ) ) );
        }
    }
    
    
    //Comments
    if( $board['enable_comments'] )
    {
        //Delete comment
        if( $core->_GET( 'cid' ) )
        {
            $comment_id = (int) $core->_GET( 'cid' );
            $comment_data = $user->commentData( $comment_id );
            
            if( $comment_data['comment_author'] == $session['user_id'] || $comment_data['comment_profile'] == $session['user_id'] || $session['group_level'] == 'ADMIN' || $session['group_level'] == 'MOD' && $comment_data )
            {
                $deleteComment = $user->deleteComment( $comment_id );
                
                if( $deleteComment )
                {
                    $page->redirect( 'index.php?action=usercp&mode=view&u=' . $comment_data['comment_profile'], $language->string( 'comment_deleted' ) );
                }
                else
                {
                    $page->error( 'Error while deleting comment' );
                }
            }
        }
        
        //Comments pagination
        $perPage = 12;
        if( is_numeric( $core->_GET( 'page' ) ) )
        {
            $pagi = (int) $core->_GET( 'page' );
            
            if( $pagi < 1 )
            {
                $pagi = 1;
            }
        }
        else
        {
            $pagi = 1;
        }
        
        $start = ( $pagi - 1 ) * $perPage;
        
        $comments = $user->getComments( $uid, $start, $perPage );
     
        $template->setIf( 'show_comments' );
        
        $pagination = $core->pagination( $userdata['profile_comments'], 12, $pagi, 'index.php?action=usercp&mode=view&u=' . $uid . '&page={PAGE}' );
        
        if( ceil( $userdata['profile_comments'] / 12 ) > 1 )
        {
            $template->setIf( 'comments_pages' );
            
            $template->assignVar( 'pagination', $pagination );
        }
        
        if( count( $comments ) )
        {
            foreach( $comments AS $comment )
            {
                if( $session['group_level'] == 'ADMIN' || $session['group_level'] == 'MOD' || $comment['comment_profile'] == $session['user_id'] || $comment['comment_author'] == $session['user_id'] )
                {
                    $template->setLoopIf( 'can_delete', 'comments' );
                }
                
                $template->setLoop( 'comments', array( 'comment_author'  => $core->htmlFilter( $comment['username'] ),
                                                       'comment_message' => $core->htmlFilter( $comment['comment_message'] ),
                                                       'comment_date'    => $core->formatTimeStamp( $comment['comment_date'] ),
                                                       'author_link'     => 'index.php?action=usercp&mode=view&u=' . $comment['comment_author'],
                                                       'a_delete'        => 'index.php?action=usercp&mode=view&u=' . $uid . '&cid=' . $comment['comment_id'] ) );
            }
        }
        
        //Can add comment?
        if( $session['session_logged'] && $session['can_comment'] )
        {
            $template->setIf( 'add_comment' );
        }
        
        $template->assignVars( array( 'l_wrote'          => $language->string( 'wrote', 0 ),
                                      'l_add_comment'    => $language->string( 'add_comment' ),
                                      'l_comments'       => $language->string( 'profile_comments' ),
                                      'l_delete_comment' => $language->string( 'delete_comment' ) ) );
        
        
        //Add comment
        if( $core->_POST( 'addComment' ) )
        {
            $comment = $core->filterItem( $core->_POST( 'comment' ) );
            
            $errors = 0;
            $error_msg = '';
            
            if( strlen( $comment ) < 5 )
            {
                $errors++;
                $error_msg .= $language->string( 'comment_too_short' ) . '<br />';
            }
            elseif( strlen( $comment ) > 150 )
            {
                $errors++;
                $error_msg .= $language->string( 'comment_too_long' ) . '<br />';
            }
            
            if( $errors )
            {
                $template->setIf( 'errors' );
                
                $template->assignVars( array( 'l_error_occured' => $language->string( 'error_occured' ),
                                              'error_message'   => $error_msg,
                                              'comment_message' => $core->htmlFilter( $comment ) ) );
            }
            else
            {
                $flood = $core->checkCommentsFlood( $session['user_id'] );
            
                if( $flood )
                {
                    $template->setIf( 'errors' );
                    
                    $template->assignVars( array( 'l_error_occured' => $language->string( 'error_occured' ),
                                                  'error_message'   => $language->string( 'comment_flood' ),
                                                  'comment_message' => $core->htmlFilter( $comment ) ) );
                }
                else
                {
                    $addComment = $user->addComment( $uid, $session['user_id'], $db->escape( $comment ) );
                    
                    if( $addComment )
                    {
                        $page->redirect( 'index.php?action=usercp&mode=view&u=' . $uid, $language->string( 'comment_added' ) );
                    }
                    else
                    {
                        //Failed query?
                        $page->error( 'Error while inserting new comment :(' );
                    }
                }
            }
        }
        
        
        //Friends
        $friends = $user->getFriends( $uid );
        
        if( count( $friends ) )
        {
            foreach( $friends AS $friend )
            {
                if( $uid == $session['user_id'] || $session['group_level'] == 'ADMIN' )
                {
                    $template->setLoopIf( 'delete_friend', 'friends' );
                }
                
                $template->setLoop( 'friends', array( 'friend_name'     => $core->colorUsername( $friend['group_style'], $core->htmlFilter( $friend['username'] ) ),
                                                      'friend_link'     => 'index.php?action=usercp&mode=view&u=' . $friend['user_id'],
                                                      'a_friend_remove' => 'index.php?action=usercp&mode=removefriend&buddy=' . $friend['user_id'] ) );
            }
        }
    }
    
    
    $page->addBreadcrumb( 'index.php?action=usercp&mode=view&u=' . $uid, $language->string( 'user_profile' ) . ' ' . $core->htmlFilter( $userdata['username'] ) );
    
    $page->drawHeader();
    $template->draw( 'profile_view' );
    $page->drawFooter();
}
elseif( $mode == 'addfriend' )
{
    //Add friend
    $page->title = $language->string( 'friends' );
    $page->addBreadcrumb( 'index.php?action=members', $language->string( 'users' ) . ' ' . $core->htmlFilter( $userdata['username'] ) );

    
    if( !$session['session_logged'] )
    {
        $page->error( $language->string( 'you_need_to_be_logged_in' ) );
    }
    
    $buddy = (int) $core->_GET( 'buddy' );
    
    if( $buddy == $session['user_id'] )
    {
        //Buddy is the same person :D
        $page->error( $language->string( 'you_cant_be_your_own_friend' ) );
    }
    else
    {
        $friend = $user->searchFriend( $buddy, $session['user_id'] ); //Check if your friend is already on the list
        
        if( $friend )
        {
            $page->error( $language->string( 'this_person_is_on_your_friend_list' ) );
        }
        else
        {
            $addFriend = $user->addFriend( $buddy, $session['user_id'] );
            
            if( $addFriend )
            {
                $page->redirect( 'index.php?action=usercp&mode=view&u=' . $buddy, $language->string( 'friend_added' ) );
            }
            else
            {
                $page->error( 'Error while adding friend' );
            }
        }
    }
}
elseif( $mode == 'removefriend' )
{
    $page->title = $language->string( 'friends' );

    
    if( !$session['session_logged'] )
    {
        $page->error( $language->string( 'you_need_to_be_logged_in' ) );
    }
    
    $buddy = (int) $core->_GET( 'buddy' );
    
    $friend = $user->searchFriend( $buddy, $session['user_id'] ); //Check if your friend is already on the list
        
    if( !$friend )
    {
        $page->error( $language->string( 'this_person_is_not_on_your_friend_list' ) );
    }
    else
    {
        $removeFriend = $user->removeFriend( $buddy, $session['user_id'] );
        
        if( $removeFriend )
        {
            $page->redirect( 'index.php?action=usercp&mode=view&u=' . $buddy, $language->string( 'friend_removed' ) );
        }
        else
        {
            $page->error( 'Error while removing friend' );
        }
    }
}




?>